package com.example.demo.Service;


import com.example.demo.Entity.OperationLog;
import com.example.demo.Entity.User;
import com.example.demo.Repository.OperationLogRepo;
import com.example.demo.Repository.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class OperationLogServiceImpl implements OperationLogService {

    private final OperationLogRepo operationLogRepository;
    private final UserRepo userRepo;

    @Autowired
    public OperationLogServiceImpl(OperationLogRepo operationLogRepository,UserRepo userRepo) {
        this.operationLogRepository = operationLogRepository;
        this.userRepo = userRepo;
    }

    @Override
    public OperationLog createOperationLog(OperationLog operationLog) {
        return operationLogRepository.save(operationLog);
    }

    @Override
    public Optional<OperationLog> getOperationLogById(Long id) {
        return operationLogRepository.findById(id);
    }

    @Override
    public Map<String,Object> getAllOperationLogs() {
        System.out.println("连接成功");

        Map<String, Object> response = new HashMap<>();
        List<OperationLog> logList = operationLogRepository.findAll();

        if(logList.size() == 0){
            response.put("exists",false);
            response.put("message","查找结果为空");
        }else{
            response.put("exists",true);
            response.put("message","查找成功");
            response.put("userList",logList);
        }
        return response;
    }

    @Override
    public Map<String, Object> searchLogByUserName(User user) {
        Optional<User> u = userRepo.findByUsername(user.getUsername());
        Map<String,Object> response = new HashMap<>();

        if(!u.isPresent()){
            response.put("exists",false);
            response.put("message","该用户不存在");
        }else{
            List<OperationLog> logList = operationLogRepository.findByUser(u.get());
            if (logList.size() != 0) {
                response.put("exists",true);
                response.put("message","查找成功");
                response.put("logList",logList);
            } else {
                response.put("exists",false);
                response.put("message","查询结果为空");
            }
        }
        return response;
    }

    @Override
    public OperationLog updateOperationLog(Long id, OperationLog operationLog) {
        Optional<OperationLog> existingLogOptional = operationLogRepository.findById(id);
        if (existingLogOptional.isPresent()) {
            OperationLog existingLog = existingLogOptional.get();
            existingLog.setUser(operationLog.getUser());
            existingLog.setIsRuku(operationLog.getIsRuku());
            existingLog.setDateTime(operationLog.getDateTime());
            existingLog.setWarehouse(operationLog.getWarehouse());
            existingLog.setShelf(operationLog.getShelf());
            existingLog.setItem(operationLog.getItem());
            return operationLogRepository.save(existingLog);
        } else {
            return null; // Or throw an exception
        }
    }

    @Override
    public boolean deleteOperationLog(Long id) {
        if (operationLogRepository.existsById(id)) {
            operationLogRepository.deleteById(id);
            return true;
        } else {
            return false; // Or throw an exception
        }
    }
}
